﻿using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace Common.Swagger.Filter
{
    /// <summary>
    /// double/decimal的Example 
    /// </summary>
    public class DecimalSchemaFilter : ISchemaFilter
    {
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (context.MemberInfo == null)
            {
                return;
            }
            if (context.Type != typeof(decimal)
                && context.Type != typeof(double)
                && context.Type != typeof(decimal?)
                && context.Type != typeof(double?))
            {
                return;
            }
            //由于Swagger的Example在小数点类型中显示的都是0，导致前端无法区分是整数还是小数
            schema.Example = OpenApiAnyFactory.CreateFor(schema, 0.01d);
        }

    }
}
